package ia1011.zanzibar.featurestructures;
/**
 * The class represents a Variable
 * 
 * @author Alessandro Perrino (creator)
 * @author <br> Bastianelli E. emanuele.bastianelli@gmail.com (modifier)
 * @author <br> Caponi E. emiliano.caponi@gmail.com (modifier)
 * @author <br> Castellucci G. castellucci.giuseppe@gmail.com (modifier)
 * @author <br> Rossi A. berardino.viola@gmail.com (modifier)
 *  
 */
public class Variable {
	String name;
	Object value;
	
	/**
	 * Initializes an empty variable
	 */
	public Variable() {
		this.name=null;
		this.value=new FeatureStructure();
	}
	
	/**
	 * Initializes an empty variable with the specified name.
	 * @param name name of variable
	 */
	public Variable(String name) {
		this.name=name;
		this.value=new FeatureStructure();
	}
	
	/**
	 * Initializes a variable with the specified name and the specified value.
	 * @param Name name of variable
	 * @param value variable value
	 */
	public Variable(String Name, Object value) {
		this.name=Name;
		this.value=value;
	}
	
	/**
	 * Sets the value of a variable.
	 * @param value variable value
	 */
	public void setValue(Object value) {
		this.value=value;
	}
	
	/**
	 * Sets variable's name
	 * @param name
	 */
	public void setName(String name) {
		this.name=name;
	}
	
	/**
	 * Returns the name of the variable.
	 * @return name of the variable.
	 */
	public String getName() {
		return this.name;
	}
	
	
	/**
	 * Returns the value of the variable.
	 * @return value of the variable.
	 */
	public Object getValue() {
		return this.value;
	}
	
	/**
	 * Returns the type of the value contained in a variable.
	 * @return type of the value contained in a variable.
	 */
	public String getType() {
		return this.value.getClass().getName();
	}
	
	/**
	 * return true if variable is empty, false otherwise
	 * @return true if variable is empty, false otherwise
	 */
	public boolean isEmpty() {
		if (this.value instanceof FeatureStructure) {
			// if the value in the variable is an empty feature structure
			if(((FeatureStructure)(this.value)).getFields().isEmpty()) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Resets the value of the variable.
	 */
	public void clean() {
		this.setValue(new FeatureStructure());
	}
	
	/**
	 * Prints a simple representation of a variable
	 */
	public void display() {
		System.out.print("["+name+"] ");
		if (value instanceof FeatureStructure)
			((FeatureStructure)value).display();
		else if (value instanceof Variable)
			((Variable)value).display();
		else if (value instanceof String)
			System.out.println((String)value);
		else if (value != null) {
			value.toString();
			System.out.println();
		}
	}
}
